﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using System.Threading;
#endregion

namespace MonopolyGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class GameClass : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D mainBoard;
        Rectangle rect;

        Player p1;
        Dies dies;

        static int WINDOW_WIDTH = 700;
        static int WINDOW_HEIGHT = 700;
        public static Rectangle bottom = new Rectangle(50, 610, WINDOW_WIDTH, WINDOW_HEIGHT - 610);
        public static Rectangle left = new Rectangle(0, 100, 100, WINDOW_HEIGHT - 100);
        public static Rectangle top = new Rectangle(0, 0, WINDOW_WIDTH - 60, 90);
        public static Rectangle right = new Rectangle(WINDOW_WIDTH - 90, 0, 90, WINDOW_HEIGHT-20);

        Texture2D t;
       

        //prevButtonState saves the default left mouse button state - which is Released
        //so we can use that when we implement the logic for the mouse drag&drop of the pawn
        ButtonState prevButtonState = ButtonState.Released;

        bool flag = false;
        public GameClass()
            : base()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            //setting up the resolution
            graphics.PreferredBackBufferWidth=WINDOW_WIDTH;
            graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;

            
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //Create the mainboard Texture which we will use as the game background
            mainBoard = Content.Load<Texture2D>("mainBoard");
            //Create the rectangle to hold the mainboard texture
            rect = new Rectangle(0,0,graphics.PreferredBackBufferWidth,graphics.PreferredBackBufferHeight);

            //makes the mouse cursor visible
            IsMouseVisible = true;

            //Load an instance of the Player's pawn
            p1 = new Player(Content, "pawn1.png", 620, 650);
            dies = new Dies();
            


            //testing 

            t = new Texture2D(GraphicsDevice, 1, 1);
            t.SetData(new[] { Color.White });
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            //Checks if the mouse is currently over the rectangle of the pawn
            bool mouseOver = p1.rect.Contains(Mouse.GetState().X, Mouse.GetState().Y);

            //Sets the rectangle's position to follow the mouse until the left mouse button's state is "released"
            if (Mouse.GetState().LeftButton == ButtonState.Pressed && prevButtonState==ButtonState.Released && mouseOver)
            {

                dies.roll();
                
            }
                //enlarges the pawn's texture a bit when the mouse is hovering over it
            else if (mouseOver && !flag)
            {
                flag = true;
                p1.rect.Width += 10;
                p1.rect.Height += 10;
            }
            else if(!mouseOver && flag)
            {
                flag = false;
                p1.rect.Width-=10;
                p1.rect.Height-=10;
            }
            else if(dies.isRolled || dies.result!=0)
            {

                p1.Update(dies.Update());
                //dies.isRolled = false;
                

            }

            base.Update(gameTime);

        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            //begin the spritebatch transaction
            spriteBatch.Begin();

            //Draw the main board
            spriteBatch.Draw(mainBoard,rect,Color.White);

            //Draw the pawn
            p1.Draw(spriteBatch);

            //spriteBatch.Draw(t, right, Color.Black);
            //spriteBatch.Draw(t, bottom, Color.Black);
            //spriteBatch.Draw(t, left, Color.Black);
            //spriteBatch.Draw(t, top, Color.Black);

            //End spritebatch transaction
            spriteBatch.End();

            base.Draw(gameTime);

            
        }
    }
}
